package test

import com.android.build.api.transform.*
import com.android.build.gradle.internal.pipeline.TransformManager
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FileUtils
import org.gradle.api.Project

class MyClassTransform extends Transform {

    MyClassTransform() {}

    @Override
    String getName() {
        return "MyClassTransform"
    }

    @Override
    Set<QualifiedContent.ContentType> getInputTypes() {
        return TransformManager.CONTENT_CLASS
    }

    @Override
    Set<QualifiedContent.Scope> getScopes() {
        return TransformManager.SCOPE_FULL_PROJECT
    }

    @Override
    boolean isIncremental() {
        return false
    }

    @Override
    void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
        Collection<TransformInput> inputs = transformInvocation.getInputs()
        Collection<TransformInput> referenceInputs = transformInvocation.getReferencedInputs()
        TransformOutputProvider outPutProvider = transformInvocation.getOutputProvider()

        inputs.each {
            TransformInput transformInput ->

                transformInput.directoryInputs.each {

                    //主app class 复制
                    it ->
//                        deleteDirectoryClassFile(it.file)
                        filterPath(it.file)
                        def dest = outPutProvider.getContentLocation(it.name, it.contentTypes, it.scopes, Format.DIRECTORY)
                        FileUtils.copyDirectory(it.file, dest)
//                        System.out.println("==================   directoryInputs  " + it.file.absolutePath.toString() + "   ==================")
//                        filterPath(dest)
                }

                //jar包及各种依赖复制
                transformInput.jarInputs.each {
                    it ->
                        def jarName = it.name
                        def md5Name = DigestUtils.md5Hex(it.file.absolutePath)


                        if (jarName.endsWith(".jar")) {
                            jarName = jarName.substring(0, jarName.length() - 4)
                        }
                        if (!it.file.absolutePath.toString().contains("bundles/default/classes.jar")) {
                            def dest = outPutProvider.getContentLocation(jarName + md5Name, it.contentTypes, it.scopes, Format.JAR)
                            FileUtils.copyFile(it.file, dest)
                        }
                }

        }
    }

    //主app目录过滤掉指定class文件
    def deleteDirectoryClassFile(File file) {

        if (file.isDirectory()) {
            file.eachFileRecurse {
                deleteDirectoryClassFile(it)
            }
        }else {
            if (file.name.endsWith(".class")) {
//                file.delete()
            }
//            System.out.println("==================   deleteClassFile  " + file.name + "   ==================")
        }
    }

    //检查主app是否包含指定类
    def filterPath(File file) {
        if (file.isDirectory()) {
            file.eachFileRecurse {

                if (it.absolutePath.toString().endsWith("com/tenllo/basea") || it.absolutePath.toString().endsWith("com/tenllo/wish")) {
                    System.out.println("??????????????  " + it.absolutePath.toString() + "   ==================")
                    it.delete()
//                    deleteClassFile(it)
                } else {
                    filterPath(it)
                }
            }
        }
    }

    //删除类文件
    def deleteClassFile(File file) {

        if (file.isDirectory()) {
            file.eachFileRecurse {
                deleteClassFile(it)
            }
        } else {
            if (file.name.endsWith(".class")) {
                file.delete()
            }
        }
    }
}